import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    userList: [], //未接入的用户列表
    selectUser: {}, //从未接入中选中用户
    selecetUserList: [], //聊天的用户列表
    currentChat: {}, //当前聊天窗口的用户
    idStore: {
      //当前聊天窗口的所有id
      meetingId: "",
      bigMeetingId: "",
      waiterId: ""
    },
    meetings: new Map(), //小会场
    chatRecords: new Map(),
    currentCloseMeetingId: "", //当前关闭的会场ID，
    /*  */
    shortcutContent: {}, //知识库的编辑，
    showTime: "", // 服务时长
    connectTotal: 0, //用户统计
    // 大会会场0x20返回的
    smartConcatInfo: {
      talkerType: "",
      talkerId: ""
    },
    closeCurrent: false, //关闭当前会话,
    onlinComplete: false //获取online列表完成标志
  },
  mutations: {
    // 更新未介入用户列表
    setUserList(state, data) {
      state.userList = data;
    },
    setConnectTotal(state, data) {
      const { type, num } = data;
      if (type == "add") {
        state.connectTotal++;
      }
      if (type == "set") {
        state.connectTotal = num;
      }
    },
    // 切换当前聊天的用户
    setCurrentChat(state, data) {
      state.currentChat = data;
    },
    //储层当前对话的各种id的数量
    setIdStore(state, data) {
      state.idStore = { ...state.idStore, ...data };
    },
    //更新选中的用户列表的数量
    updateSelecetUserList(state, data) {
      const { type, ...arg } = data;
      if (type === "clear") {
        state.selecetUserList = [];
      }
      if (type === "add") {
        state.selecetUserList.push(arg);
      }
      if (type === "del") {
        //下线的逻辑
        state.currentCloseMeetingId = arg.meetingId;
        state.selecetUserList = state.selecetUserList.filter(
          item => item.meetingId != arg.meetingId
        );
      }
    },
    //更新存储小会场ws连接的MAP
    updateMeeting(state, data) {
      const { type, ...arg } = data;
      if (type == "clear") {
        state.meetings.clear();
      }
      if (type == "add") {
        state.meetings.set(arg.key, arg.value);
      }
      if (type == "del") {
        state.meetings.delete(arg.key);
      }
    },
    //更新用列表的未读信息状态
    updateSelecetUserListSate(state, data) {
      const { type, ...arg } = data;
      if (type == "text" || type == "img") {
        state.selecetUserList.forEach(item => {
          if (item.meetingId === arg.meetingId) {
            item.lastTxt = arg.lastTxt;
            item.lastTime = arg.lastTime;
          }
        });
      }
      if (type == "unread") {
        state.selecetUserList.forEach(item => {
          if (item.meetingId === arg.meetingId) {
            item.unread += 1;
          }
        });
      }
      if (type == "offline") {
        state.selecetUserList.forEach(item => {
          if (item.meetingId === arg.meetingId) {
            item.offline = true;
          }
        });
      }
      if (type == "reset") { 
        state.selecetUserList.forEach(item => {
          if (item.meetingId === arg.meetingId) {
            item = Object.assign(item,arg);
          }
        });
      }
    },
    //更新会场对应的聊天记录
    updateChatRecords(state, data) {
      const { type } = data;
      if (type == "add") {
        state.chatRecords.set(data.key, data.value);
      }
      if (type == "del") {
        state.chatRecords.delete(data.key);
      }
    },
    // 知识库内容编辑
    setShortcutContent(state, data) {
      state.shortcutContent = data;
    },
    //计算服务时长
    setServiceTime(state, data) {
      state.showTime = data;
    },
    //
    setSmartConcatInfo(state, data) {
      state.smartConcatInfo = Object.assign(state.smartConcatInfo, data);
    },
    // 设置会场关闭标识
    setCloseCurrent(state, data) {
      state.closeCurrent = data;
    },
    // 设置获取在线列表完成标识
    setOnlinComplete(state, data) {
      state.onlinComplete = data;
    }
  },

  getters: {
    selecetUserNum(state) {
      //接入用户的数量
      return state.selecetUserList.length;
    }
  }
});

export default store;
